﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.Core
{
    public class RangeManager
    {
        public int Size { get; set; }
        public int Start { get; set; }
        public int End { get; set; }
        public bool Stop { get; set; }

        RangeManager m_range = null;

        public RangeManager()
        {

        }

        public RangeManager(int size)
        {
            Size = size;
            Start = 1;
            End = size;
            Stop = false;

            m_range = this;
        }

        public RangeManager(int start, int end)
        {
            Size = (end - start) + 1;
            Start = start;
            End = end;
            Stop = false;

            m_range = this;
        }

        public RangeManager(int size, AllineamentoStatusBag status)
        {
            if (status != null)
            {
                Start = status.Range.Start;
                End = status.Range.End;
            }
            else
            {
                Start = 1;
                End = size;
            }

            Size = size;
            Stop = false;
            m_range = this;
        }

        public static RangeManager Create(int size)
        {
            RangeManager p = new RangeManager(size);

            return p;
        }

        public static RangeManager Create(int size, AllineamentoStatusBag status)
        {
            RangeManager p = new RangeManager(size, status);
            
            return p;
        }

        public void Iterate(Action<int> action)
        {
            Stop = false;
            for (int i = m_range.Start; i <= m_range.End; i++)
            {
                action(i);

                if (Stop)
                {
                    break;
                }
            }
        }

        public RangeManager Next()
        {
            m_range = new RangeManager(m_range.Start + Size, m_range.End + Size);

            return m_range;
        }

        public override string ToString()
        {
            string result = string.Empty;

            result = string.Format("S:{0} - E:{1}, Sz:{2}", this.Start, this.End, this.Size);

            return result;
        }
    }
}
